<?php
//定义一个类
class Db{
    private $host;
    private $root;
    private $pwd;
    private $dbname;
    private $port;
    private $link; // 数据库对象
    public function __construct($params)
    {
        // 初始化属性
        $this->init($params);
        // 创建数据库对象

    }
    // 初始化属性
    private function init($params=array()){
        $this->host=$params['host']??'localhost'; // 是否设置了 host
        $this->root = $params['root']??'root';
        $this->pwd=$params['pwd']??'123';
        $this->dbname=$params['dbname'];
        $this->port=$params['port']??'3306';
        $this->charset=$params['charset']??'utf8';
    }
    // 创建数据库对象
    private function connect(){
        $this->link=@mysqli_connect($this->host,$this->root,$this->pwd,$this->dbname,$this->port);
        if($this->link){
            echo "数据库连接失败".mysqli_connect_error();
            exit;
        }
    }
    // 执行 增删改查
    public function execute($sql){
        if(!$str = mysqli_query($this->link,$sql)){
            echo "sql语句执行错误".mysqli_error($this->link);
            echo "<br>错误的sql语句是".$sql;
            exit;
        }
        return $str;
    }
}
$str =[
    "host"=>"127.0.0.1",
    "root"=>"root",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
    "pwd"=>"root",
    'dbname'=>'test'
];


?>